package model

import (
	"cloud-disk/core/helper"
	"cloud-disk/xorm"
	"errors"
	"time"
)

type UserRepostiory struct {
	Id                 uint64
	Identity           string
	UserIdentity       string
	ParentId           int
	ReoisitoryIdentity string
	Ext                string
	Name               string
	CreatedAt          time.Time `xorm:"created"`
	UpdatedAt          time.Time `xorm:"updated"`
	DeletedAt          time.Time `xorm:"deleted"`
}

//TableName 指定表名
func (table UserRepostiory) TableName() string {
	return "user_repostiory"
}

//CreateUserRepostiory 创建数据
func (table *UserRepostiory) CreateUserRepostiory(ur *UserRepostiory) error {
	_, err := xorm.Xorm.Insert(ur)
	if err != nil {
		return err
	}
	return nil
}

// GetUserFilesList 查询用户文件列表
func (table *UserRepostiory) GetUserFilesList(parentId int, userIdentity string, size, page int) (urs []*UserRepostiory, err error) {
	urs = make([]*UserRepostiory, 0)
	err = xorm.Xorm.Where("parent_id = ? and user_identity = ?", parentId, userIdentity).
		Limit(size, page).
		Find(&urs)
	if err != nil {
		return nil, err
	}
	return urs, nil
}

// GetUserFileCount 查询用户文件个数
func (table *UserRepostiory) GetUserFileCount(parentId int, userIdentity string) (count int64, err error) {
	count, err = xorm.Xorm.Where("parent_id = ? and user_identity = ?", parentId, userIdentity).Count(new(UserRepostiory))
	if err != nil {
		return 0, err
	}
	return count, nil
}

//getThisNameFileByName 判断当前目录是否有同名文件
func (table *UserRepostiory) getThisNameFileByNameCount(name, identity, userIdentity string) (int64, error) {
	count, err := xorm.Xorm.Where("name=? and parent_id = (select parent_id from user_repostiory ur where ur.identity = ?) and user_identity = ?", name, identity, userIdentity).Count(new(UserRepostiory))
	if err != nil {
		return 0, err
	}

	return count, nil
}

//UpdateFileNameByIdentity 更改文件名
func (table *UserRepostiory) UpdateFileNameByIdentity(identity, userIdentity, name string) error {
	count, err := table.getThisNameFileByNameCount(name, identity, userIdentity)
	if err != nil {
		return err
	}

	if count != 0 {
		return errors.New("文件名称已经存在")
	}

	ur := new(UserRepostiory)
	get, err := xorm.Xorm.Where("identity = ? and user_identity = ?", identity, userIdentity).Get(ur)
	if err != nil {
		return err
	}
	if !get {
		return errors.New("无此数据")
	}

	ur.Name = name
	update, err := xorm.Xorm.Where("identity = ? and user_identity = ?", identity, userIdentity).Update(ur)
	if err != nil {
		return err
	}
	if update == 0 {
		return errors.New("更新失败")
	}
	return nil
}

//CreateFolder 创建文件夹
func (table *UserRepostiory) CreateFolder(name, userIdentity string, parentId int64) (ur *UserRepostiory, err error) {
	count, err := xorm.Xorm.Where("name=? and parent_id =  ? and user_identity = ?", name, parentId, userIdentity).Count(new(UserRepostiory))
	if err != nil {
		return nil, err
	}

	if count != 0 {
		return nil, errors.New("文件名称已经存在")
	}

	ur = new(UserRepostiory)
	ur.Identity = helper.GetUUID()
	ur.ParentId = int(parentId)
	ur.Name = name
	ur.UserIdentity = userIdentity

	_, err = xorm.Xorm.Insert(ur)
	if err != nil {
		return nil, err
	}

	return ur, nil
}

//DeleteFileByIdentity 删除文件
func (table *UserRepostiory) DeleteFileByIdentity(identity, userIdentity string) error {
	_, err := xorm.Xorm.Where("identity = ? and user_identity = ?", identity, userIdentity).Delete(new(UserRepostiory))
	if err != nil {
		return err
	}

	return nil
}

//GetFileByIdentity 根据identity 获取文件
func (table *UserRepostiory) GetFileByIdentity(identity string) (ur *UserRepostiory, has bool, err error) {
	ur = new(UserRepostiory)
	xorm.Xorm.ShowSQL(true)
	has, err = xorm.Xorm.Where("identity = ?", identity).Get(ur)
	if err != nil {
		return nil, false, err
	}
	return ur, has, nil
}

//UpdateFileParentIdByIdentity 更新ParentId
func (table *UserRepostiory) UpdateFileParentIdByIdentity(identity, userIdentity string, parentId int) error {
	// 先查询出文件
	ur, has, err := table.GetFileByIdentity(identity)
	if err != nil {
		return err
	}
	if !has {
		return errors.New("数据不存在")
	}

	// 判断这个目录是否有同名数据
	count, err := xorm.Xorm.Where("name=? and parent_id = ? and user_identity = ?", ur.Name, parentId, userIdentity).Count(new(UserRepostiory))
	if err != nil {
		return err
	}

	if count != 0 {
		return errors.New("文件名称已经存在")
	}

	// 正式更新
	ur.ParentId = parentId
	_, err = xorm.Xorm.Where("identity = ? and user_identity = ?", identity, userIdentity).Update(ur)
	if err != nil {
		return err
	}
	return nil
}
